/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhaojiu.service.impl;

import me.zhengjie.modules.zhaojiu.domain.AttendanceDetail;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhaojiu.repository.AttendanceDetailRepository;
import me.zhengjie.modules.zhaojiu.service.AttendanceDetailService;
import me.zhengjie.modules.zhaojiu.service.dto.AttendanceDetailDto;
import me.zhengjie.modules.zhaojiu.service.dto.AttendanceDetailQueryCriteria;
import me.zhengjie.modules.zhaojiu.service.mapstruct.AttendanceDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2021-12-09
**/
@Service
@RequiredArgsConstructor
public class AttendanceDetailServiceImpl implements AttendanceDetailService {

    private final AttendanceDetailRepository attendanceDetailRepository;
    private final AttendanceDetailMapper attendanceDetailMapper;

    @Override
    public Map<String,Object> queryAll(AttendanceDetailQueryCriteria criteria, Pageable pageable){
        Page<AttendanceDetail> page = attendanceDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(attendanceDetailMapper::toDto));
    }

    @Override
    public List<AttendanceDetailDto> queryAll(AttendanceDetailQueryCriteria criteria){
        return attendanceDetailMapper.toDto(attendanceDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AttendanceDetailDto findById(Long attDetialId) {
        AttendanceDetail attendanceDetail = attendanceDetailRepository.findById(attDetialId).orElseGet(AttendanceDetail::new);
        ValidationUtil.isNull(attendanceDetail.getAttDetialId(),"AttendanceDetail","attDetialId",attDetialId);
        return attendanceDetailMapper.toDto(attendanceDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AttendanceDetailDto create(AttendanceDetail resources) {
        return attendanceDetailMapper.toDto(attendanceDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AttendanceDetail resources) {
        AttendanceDetail attendanceDetail = attendanceDetailRepository.findById(resources.getAttDetialId()).orElseGet(AttendanceDetail::new);
        ValidationUtil.isNull( attendanceDetail.getAttDetialId(),"AttendanceDetail","id",resources.getAttDetialId());
        attendanceDetail.copy(resources);
        attendanceDetailRepository.save(attendanceDetail);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long attDetialId : ids) {
            attendanceDetailRepository.deleteById(attDetialId);
        }
    }

    @Override
    public void download(List<AttendanceDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AttendanceDetailDto attendanceDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("考勤ID", attendanceDetail.getAttendanceId());
            map.put("学生ID", attendanceDetail.getStudentId());
            map.put("是否签到（0：缺勤，1：请假，2：正常）", attendanceDetail.getIsCheckIn());
            map.put("创建者", attendanceDetail.getCreateBy());
            map.put("更新者", attendanceDetail.getUpdateBy());
            map.put("创建日期", attendanceDetail.getCreateTime());
            map.put("更新时间", attendanceDetail.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
